package com.turing.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.turing.system.utils.FileUtils;
import constants.FileConstants;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import result.Result;

@RestController
@RequestMapping("file")
public class FileController {

    /**
     *  @author: shihonggang
     *  @Date: 2023/5/5 15:09
     *  @Description: 文件上传
     */
    @PostMapping(value = "fileUpload")
    public Result fileUpload(@RequestParam(value = "file") MultipartFile file){
        String fileName = FileUtils.upload(FileConstants.PATH, file);
        return Result.success(FileConstants.URL+fileName);
    }

    /**
     *  @author: shihonggang
     *  @Date: 2023/5/10 16:22
     *  @Description: 富文本文件上传
     */
    @PostMapping(value = "simditorFileUpload")
    public JSONObject simditorFileUpload(@RequestParam(value = "file") MultipartFile file){
        String fileName = FileUtils.upload(FileConstants.PATH, file);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("success", true);
        jsonObject.put("file_path", FileConstants.URL+fileName);
        return jsonObject;
    }
}
